package NC27_集合的所有子集1;

import java.util.ArrayList;
import java.util.Arrays;

public class Solution {
    public ArrayList<ArrayList<Integer>> subsets(int[] S) {
        ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
        res.add(new ArrayList<>());
        ArrayList<Integer> list = new ArrayList<>();
        Arrays.sort(S);
        dfs(0, S, res, list);
        return res;
    }

    private void dfs(int start, int[] S, ArrayList<ArrayList<Integer>> res, ArrayList<Integer> list) {
        if (start >= S.length){
            return;
        }

        for (int i = start; i < S.length; i++){
            list.add(S[i]);
            res.add(new ArrayList<>(list));
            dfs(i + 1, S, res, list);

            list.remove(list.size() - 1);
        }
    }

}